package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    /**
     * 新增床位
     * @param bedDto
     */
    @Override
    public void addBed(BedDto bedDto) {
        Bed bed = new Bed();
        BeanUtil.copyProperties(bedDto,bed);
        bed.setBedStatus(0); // 入住状态, 默认未入住
        bed.setCreateTime(LocalDateTime.now());
        bed.setCreateBy(1L);

        try {
            bedMapper.addBed(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    /**
     * 根据id查询床位
     * @param id
     * @return
     */
    @Override
    public BedVo getBedById(Long id) {
        Bed bed = bedMapper.getBedById(id);
        BedVo bedVo = new BedVo();
        //                          源对象  目标对象
        // BeanUtil.copyProperties(source,target);
        BeanUtils.copyProperties(bed,bedVo); // 把bed对象中的对应属性的值复制到bedVo对象中
        return bedVo;
    }

    /**
     * 更新床位
     * @param bedDto
     */
    @Override
    public void updateBed(BedDto bedDto) {
        Bed bed = new Bed();
        //                          源对象  目标对象
        // BeanUtil.copyProperties(source,target);
        BeanUtil.copyProperties(bedDto,bed); // 把bedDto对象中的对应属性的值复制到bed对象中
        bedMapper.updateBed(bed);
    }

    /**
     * 删除床位
     * @param id
     */
    @Override
    public void deleteBed(Long id) {
        bedMapper.deleteBed(id);
    }

}

